package cn.gdk.gaodekecc.service.impl;

import cn.gdk.gaodekecc.domain.PurchaseApply;
import cn.gdk.gaodekecc.domain.StockLogs;
import cn.gdk.gaodekecc.domain.StockWarLog;
import cn.gdk.gaodekecc.domain.Stocks;
import cn.gdk.gaodekecc.exception.GlobalExceptionCodeEunm;
import cn.gdk.gaodekecc.exception.LsuExeception;
import cn.gdk.gaodekecc.feig.SystemdictionarytypeServer;
import cn.gdk.gaodekecc.mapper.StockLogsMapper;
import cn.gdk.gaodekecc.mapper.StockWarLogMapper;
import cn.gdk.gaodekecc.mapper.StocksMapper;
import cn.gdk.gaodekecc.service.IStocksService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-20
 */
@Service
public class StocksServiceImpl extends ServiceImpl<StocksMapper, Stocks> implements IStocksService {
    @Autowired
    private StocksMapper mStocksMapper;
    @Autowired
    private StockWarLogMapper stockWarLogMapper;
    @Autowired
    private StockLogsMapper stockLogsMapper;
    @Autowired
    private SystemdictionarytypeServer  systemdictionarytypeServer;
    @Value("${lssue.threshold}")
    private  Long threshold;
    @Value("${lssue.process}")
    private  Integer pro;

    /**
     * 入库
     * @param stocks
     */
    @Override
    public void warehousing(Stocks stocks) {
        String productName = stocks.getProductName();
        Stocks stock=mStocksMapper.findByName(productName);
        if(stock==null){
            mStocksMapper.add(stocks);
            log(stocks);

        }else {
            stock.setCount(stock.getCount()+stocks.getCount());
            mStocksMapper.updateById(stock);
            // 添加库存日志表
            log(stocks);
        }


    }

    private void log(Stocks stocks) {
        // 添加库存日志表
        StockLogs stockLogs = new StockLogs();
        stockLogs.setCount(stocks.getCount());
        stockLogs.setCreTime(new Date());
        stockLogs.setProductId(stocks.getProductId());
        stockLogs.setProductName(stocks.getProductName());
        stockLogs.setStaus(1);
        stockLogs.setTypeId(stocks.getTypeId());
        stockLogsMapper.insert(stockLogs);
        // 库存警告日志表
        if(stocks.getCount()<threshold){
            StockWarLog stockWarLog = new StockWarLog();
            stockWarLog.setCreateTime(new Date());
            stockWarLog.setProductName(stocks.getProductName());
            stockWarLog.setProductId(stocks.getProductId());
            stockWarLog.setInfo("新增商品数量低于阈值");
            stockWarLogMapper.insert(stockWarLog);
            throw new LsuExeception(GlobalExceptionCodeEunm.LSSUE);

        }
    }

    /**
     * 出库
     * @param stocks
     */
    @Override
    public void issue(Stocks stocks) {
        String productName = stocks.getProductName();
        Stocks stock=mStocksMapper.findByName(productName);
        if(stock.getCount()>stocks.getCount()){
            stock.setCount(stock.getCount()-stocks.getCount());
            mStocksMapper.updateById(stock);
            // 添加库存日志表
            StockLogs stockLogs = new StockLogs();
            stockLogs.setCount(stocks.getCount());
            stockLogs.setUpTime(new Date());
            stockLogs.setProductId(stocks.getProductId());
            stockLogs.setProductName(stocks.getProductName());
            stockLogs.setStaus(1);
            stockLogs.setTypeId(stocks.getTypeId());
            stockLogsMapper.insert(stockLogs);


            // 库存警告日志表
            if(stocks.getCount()<threshold){
                StockWarLog stockWarLog = new StockWarLog();
                stockWarLog.setCreateTime(new Date());
                stockWarLog.setProductName(stocks.getProductName());
                stockWarLog.setProductId(stocks.getProductId());
                stockWarLog.setInfo("出库后商品数量低于阈值");
                stockWarLogMapper.insert(stockWarLog);
                throw new LsuExeception(GlobalExceptionCodeEunm.LSSUE);

            }

        }
        if(stock.getCount()-stocks.getCount()<threshold){
            StockWarLog stockWarLog = new StockWarLog();
            stockWarLog.setCreateTime(new Date());
            stockWarLog.setProductName(stocks.getProductName());
            stockWarLog.setProductId(stocks.getProductId());
            stockWarLog.setInfo("出库后商品数量低于阈值");
            stockWarLogMapper.insert(stockWarLog);
            throw new LsuExeception(GlobalExceptionCodeEunm.LSSUE);

        }
        if(stock.getCount()<pro){
            PurchaseApply purchaseApply = new PurchaseApply();
            purchaseApply.setCount(pro);
            purchaseApply.setProductId(stock.getProductId());
            purchaseApply.setProductName(stock.getProductName());
            systemdictionarytypeServer.purchaseApply(purchaseApply);
        }

    }

    @Override
    public Stocks selectBytypeId(Integer typeId) {
        return mStocksMapper.selectBytypeId(typeId);
    }
}
